﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Aga.GestionDeArchivos.Web;
using System.Web.Routing;


namespace System.Web.Mvc.Html
{
    using Aga.GestionDeArchivos.Services;
    using Aga.GestionDeArchivos.Domain.Entities;

    public static class HtmlHelperExtentions
    {
        public static bool UserCan(this HttpContextBase context, Actions requiredAction)
        {
            if (context.Request.IsAuthenticated)
            {
                var user = (User)context.Session[SessionKey.LoggedInUser];

                UserService service = new UserService();

                return service.UserCan(user.Id, (int)requiredAction);
            }

            return false;
        }

        public static MvcHtmlString SecureActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, Actions requiredAction)
        {
            if (!htmlHelper.ViewContext.HttpContext.UserCan(requiredAction))
                return new MvcHtmlString(string.Empty);

            return htmlHelper.ActionLink(linkText, actionName);            
        }

        public static MvcHtmlString SecureActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues, Actions requiredAction)
        {
            if (!htmlHelper.ViewContext.HttpContext.UserCan(requiredAction))
                return new MvcHtmlString(string.Empty);

            return htmlHelper.ActionLink(linkText, actionName, routeValues);
        }

        public static MvcHtmlString SecureActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, RouteValueDictionary routeValues, Actions requiredAction)
        {
            if (!htmlHelper.ViewContext.HttpContext.UserCan(requiredAction))
                return new MvcHtmlString(string.Empty);

            return htmlHelper.ActionLink(linkText, actionName, routeValues);
        }

        public static MvcHtmlString SecureActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, Actions requiredAction)
        {
            if (!htmlHelper.ViewContext.HttpContext.UserCan(requiredAction))
                return new MvcHtmlString(string.Empty);

            return htmlHelper.ActionLink(linkText, actionName, controllerName);
        }

        public static MvcHtmlString SecureActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues, object htmlAttributes, Actions requiredAction)
        {
            if (!htmlHelper.ViewContext.HttpContext.UserCan(requiredAction))
                return new MvcHtmlString(string.Empty);

            return htmlHelper.ActionLink(linkText, actionName, routeValues, htmlAttributes);
        }

        public static MvcHtmlString SecureActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes, Actions requiredAction)
        {
            if (!htmlHelper.ViewContext.HttpContext.UserCan(requiredAction))
                return new MvcHtmlString(string.Empty);

            return htmlHelper.ActionLink(linkText, actionName, routeValues, htmlAttributes);
        }

        public static MvcHtmlString SecureActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes, Actions requiredAction)
        {
            if (!htmlHelper.ViewContext.HttpContext.UserCan(requiredAction))
                return new MvcHtmlString(string.Empty);

            return htmlHelper.ActionLink(linkText, actionName, routeValues, htmlAttributes);
        }

        public static MvcHtmlString SecureActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes, Actions requiredAction)
        {
            if (!htmlHelper.ViewContext.HttpContext.UserCan(requiredAction))
                return new MvcHtmlString(string.Empty);

            return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes);
        }


    }
}